package model

import (
	"choujiang/common/db"
	"fmt"
)

type FishUserExtra struct {
	Id              int64  `xorm:"INT(11) not null pk autoincr" json:"id"`
	UserId          int64  `xorm:"not null default 0 INT(11) index" json:"user_id"`
	UniqueId        string `xorm:"VARCHAR(50)" json:"unique_id"`
	ParentId        int64  `xorm:"INT(11) not null default 0" json:"parent_id"`
	Coins           int64  `xorm:"not null default 0 INT(11)" json:"coins"`
	Diamond         int64  `xorm:"not null default 0 INT(11)" json:"diamond"`
	LastExit        int64  `xorm:"not null default 0 INT(11)" json:"last_exit"`
	FishSum         int64  `xorm:"not null default 0 INT(11)" json:"fish_sum"`
	LotteryNum      int64  `xorm:"not null default 0 INT(11)" json:"lottery_num"`
	LastLottery     int64  `xorm:"not null default 0 INT(11)" json:"last_lottery"`
	InviteNum       int64  `xorm:"not null default 0 INT(11)" json:"invite_num"`
	City            int64  `xorm:"not null default 0 INT(11)" json:"city"`
	Province        int64  `xorm:"not null default 0 INT(11)" json:"province"`
	IpAddr          string `xorm:"VARCHAR(20)" json:"ip_addr"`
	TotalVideo      int64  `xorm:"not null default 0 INT(11)" json:"total_video"`
	Lucky           int64  `xorm:"not null default 0 INT(11)" json:"lucky"`
	TitleExperience int64  `xorm:"not null default 0 INT(11)" json:"title_experience"`
	PkOpen          int64  `xorm:"not null default 0 INT(11)" json:"pk_open"`
	PkScore         int64  `xorm:"not null default 0 INT(11)" json:"pk_score"`
	CreatedAt       int64  `xorm:"not null default 0 INT(11) created" json:"created_at"`
	UpdatedAt       int64  `xorm:"not null default 0 INT(11) updated" json:"updated_at"`
}

func (u *FishUserExtra) Insert() (*FishUserExtra, error) {
	_, err := db.MysqlEngine.InsertOne(u)
	return u, err
}

func (u *FishUserExtra) Update() (*FishUserExtra, error) {
	_, err := db.MysqlEngine.ID(u.Id).Update(u)
	return u, err
}

func (u *FishUserExtra) CoinsReduce(coins int64) (*FishUserExtra, error) {
	var (
		err      error
		newCoins int64
	)
	if newCoins = u.Coins - coins; newCoins < 0 {
		newCoins = 0
	}
	if _, err = db.MysqlEngine.Exec("UPDATE fish_user_extra SET coins=? WHERE id=?", newCoins, u.Id); err != nil {
		fmt.Printf("CoinsReduce err 1: %s", err)
		return u, err
	}
	return u, nil
}

func (u *FishUserExtra) CoinsIncrease(coins int64) (*FishUserExtra, error) {
	var (
		err      error
		newCoins int64
	)
	if newCoins = u.Coins + coins; newCoins < 0 {
		newCoins = 0
	}
	if _, err = db.MysqlEngine.Exec("UPDATE fish_user_extra SET coins=? WHERE id=?", newCoins, u.Id); err != nil {
		fmt.Printf("CoinsReduce err 1: %s", err)
		return u, err
	}
	return u, nil
}

func (u *FishUserExtra) TitleExpIncrease(exp int64) (*FishUserExtra, error) {
	var (
		newExp int64
		err    error
	)
	if newExp = u.TitleExperience + exp; newExp < 0 {
		newExp = 0
	}
	if _, err = db.MysqlEngine.Exec("UPDATE fish_user_extra SET title_experience = ? WHERE id=?", newExp, u.Id); err != nil {
		fmt.Printf("TitleExpIncrease err 3: %s", err)
		return u, err
	}
	return u, nil
}

func GetFishExtraByUniqueId(uniqueId string) *FishUserExtra {
	extra := new(FishUserExtra)
	has, err := db.MysqlEngine.Where("unique_id=?", uniqueId).Get(extra)
	if !has || err != nil {
		fmt.Printf("GetFishExtraByUniqueId err %+v\n", err)
		return nil
	}
	return extra
}
